package com.itguigu.spzx.common.interceptor;

import com.itguigu.spzx.common.entity.LoginUser;
import com.itguigu.spzx.common.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author lxr
 * @date 2025/10/21 19:23
 * 获取个人信息的拦截器
 */
@Component
public class UserInfoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("--------------------------请求拦截器执行了-------------------------");
        String id = request.getHeader("userId");
        LoginUser user = new LoginUser(id,null);
        ThreadLocalUtil.threadLocal.set(user);

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ThreadLocalUtil.threadLocal.remove();
    }
}
